package cn.qiuyu.gift.controller;

import cn.qiuyu.gift.service.ITenantService;
import cn.qiuyu.gift.domain.Tenant;
import cn.qiuyu.gift.query.TenantQuery;
import cn.qiuyu.gift.util.AjaxResult;
import cn.qiuyu.gift.util.PageList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.plugins.Page;

import java.util.List;

@RestController
@RequestMapping("/tenant")
public class TenantController {
    @Autowired
    public ITenantService tenantService;


    /**
     * 保存和修改公用的
     * @param tenant  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Tenant tenant){

        if( tenant.getId()!=null)
            tenantService.updateById(tenant);
        else
            tenantService.insert(tenant);
        return AjaxResult.success();

    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
            tenantService.deleteById(id);
        return AjaxResult.success();

    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {

        Tenant tenant = tenantService.selectById(id);
        return AjaxResult.success(tenant);

    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        List< Tenant> list = tenantService.selectList(null);
        return AjaxResult.success(list);

    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult json(@RequestBody TenantQuery query)
    {
        Page<Tenant> page = new Page<Tenant>(query.getPage(),query.getRows());
        page = tenantService.selectPage(page);
        PageList pageList = new PageList<Tenant>(page.getTotal(),page.getRecords());
        return AjaxResult.success(pageList);
    }

    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Tenant query)
    {

        tenantService.settlement(query);

        return AjaxResult.success();
    }





}
